home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / libs / mufs_usergroup.lha / usergroup / loginname.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  4KB  |  142 lines

  1. RCS_ID_C="$Id: loginname.c,v 2.1 1994/02/17 02:21:58 ppessi Exp $";
  2. /*
  3.  * loginname.c - Login name handling
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP User Library.
  8.  *
  9.  * Copyright ⌐ 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Sun Nov 28 17:45:55 1993 ppessi
  13.  * Last modified: Thu Jan 27 08:39:51 1994 ppessi
  14.  *
  15.  * $Log: loginname.c,v $
  16.  * Revision 2.1  1994/02/17  02:21:58  ppessi
  17.  * *** empty log message ***
  18.  *
  19.  * Revision 1.2  1994/01/21  08:13:47  ppessi
  20.  * Updated documentation
  21.  *
  22.  * Revision 1.1  1994/01/19  10:05:48  ppessi
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27. #include "base.h"
  28. #include "credential.h"
  29. #include "libfunc.h"
  30. #include <string.h>
  31. #include <exec/memory.h>
  32.  
  33. /****** usergroup.library/getlogin *******************************************
  34.  
  35.     NAME
  36.         getlogin - get login name
  37.  
  38.     SYNOPSIS
  39.         name = getlogin()
  40.         D0
  41.  
  42.         char *getlogin(void)
  43.  
  44.     FUNCTION
  45.         The getlogin() routine returns the login name of the user associated
  46.         with the current session, as previously set by setlogin().  The name
  47.         is normally associated with a console at the time a session is
  48.         created, and is inherited by all processes descended from the login
  49.         process.  (This is true even if some of those processes assume
  50.         another user ID, for example when su is used.)
  51.  
  52.     INPUTS
  53.  
  54.     RESULT
  55.         name   - pointer to login name
  56.  
  57.     SEE ALSO
  58.         setlogin()
  59.  
  60. ****************************************************************************
  61. */
  62.  
  63. SAVEDS ASM char *R_getlogin(void)
  64. {
  65.   static char buffer[MAXLOGNAME];
  66.  
  67.   struct proc *p;
  68.  
  69.   lock(proc_list);
  70.   p = procfind(NULL);
  71.   memcpy(buffer, p->p_session->s_login, MAXLOGNAME);
  72.   unlock(proc_list);
  73.  
  74.   return buffer;
  75. }
  76.  
  77. /****** usergroup.library/setlogin *******************************************
  78.  
  79.     NAME
  80.         setlogin - set login name
  81.  
  82.     SYNOPSIS
  83.         success = setlogin(name)
  84.         D0                 A1
  85.  
  86.         int setlogin(const char *);
  87.  
  88.     FUNCTION
  89.         The function setlogin() sets the login name of the user associated
  90.         with the current session to name. This call is restricted to the
  91.         super-user, and is normally used only when a new session is being
  92.         created on behalf of the named user (for example, at login time, or
  93.         when a remote shell is invoked).
  94.  
  95.     INPUTS
  96.         name     - Buffer to hold login name
  97.  
  98.     RESULT
  99.         If a call to setlogin() succeeds, a value of 0 is returned.  If
  100.         setlogin() fails, a value of -1 is returned and an error code is
  101.         placed into global errno location.
  102.  
  103.     ERRORS
  104.         [EPERM]  - The caller has got no necessary privileges.
  105.         [EFAULT] - The name parameter gave an invalid address.
  106.         [EINVAL] - The name parameter pointed to a string that was too long.
  107.                    Login names are limited to MAXLOGNAME (from <sys/param.h>)
  108.                    characters, currently 16.
  109.  
  110.     BUGS
  111.  
  112.     SEE ALSO
  113.         getlogin()
  114.  
  115. **********************************************************************
  116. */
  117.  
  118. SAVEDS ASM int R_setlogin(REG(a1) const char *buffer)
  119. {
  120.   struct proc *p;
  121.   int error;
  122.  
  123.   if (buffer == NULL) {
  124.     error = EFAULT;
  125.   } else if (strlen(buffer) > MAXLOGNAME - 1) {
  126.     error = EINVAL;
  127.   } else {
  128.     p = procfind(NULL);
  129.  
  130.     error = suser(p->p_cred->pc_ucred);
  131.  
  132.     if (!error) {
  133.       strncpy(p->p_session->s_login, buffer, MAXLOGNAME);
  134.       p->p_session->s_login[MAXLOGNAME - 1] = '\0';
  135.       return 0;
  136.     }
  137.   }
  138.  
  139.   SetErrno(error);
  140.   return -1;
  141. }
  142.